
import React, { useEffect } from "react";
import { useLocation, useNavigate, } from "react-router-dom";
import Routers from "./route-component";
import { getToken } from "@/utils";
import { toJS } from "mobx";
import { mobxState } from "@/data";
import { observer } from "mobx-react-lite";


// 去首页
function ToHome() {
    const navigate = useNavigate();
    useEffect(() => {
        navigate("/")
    }, [navigate])
    return <></>
}
// 去登录页面
function ToLogin() {
    const navigate = useNavigate();
    useEffect(() => {
        navigate("/login");
    }, [navigate])
    return <></>
}

/**
 * 😄路由校验
 * @param {} param0 
 * @returns 
 */
const AuthRouter = () => {
    let routes = toJS(mobxState.routes)
    const location = useLocation();
    let token = getToken();
    if (location.pathname === "/login" && token) {
        return <ToHome />
    }
    if (location.pathname !== "/login" && !token) {
        return <ToLogin />
    }
    return <Routers routes={routes}  />
}
export default observer(AuthRouter);

